package com.itheima.web.listener;

import com.itheima.autoconfig.huawei.HuaWeiUGCProperties;
import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 动态审核监听器
@Component
@RocketMQMessageListener(consumerGroup = "consumer-movement-group", topic = "tanhua-movement")
public class MovementCheckListener implements RocketMQListener<String> {

    @Reference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String movementId) {
        System.out.println("获取动态id，开启审核：" + movementId);

        // 2.查询动态详情
        Movement movement = movementService.findMovementById(movementId);

        // 3.审核
        if (movement!=null) {
            // 3-1 文本
            String text = movement.getTextContent();
            boolean textCheck = huaWeiUGCTemplate.textContentCheck(text);
            // 3-2 图片
            String[] imgArray = movement.getMedias().toArray(new String[]{});
            boolean imgCheck = huaWeiUGCTemplate.imageContentCheck(imgArray);

            // 3-3根据审核结果进行处理
            if (textCheck && imgCheck) {
                // 审核通过
                movement.setState(1);
            }else{
                // 审核驳回
                movement.setState(2);
            }

            // 3-4更新动态
            movementService.updateMovementState(movement);
        }
    }
}
